package pers.chao.cacheable.infrastructure.utils;

import pers.chao.cacheable.core.consts.EvictEqType;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Description: redis数据结构用到的类型转换
 *
 * @author W.Chao
 * @date 2020/12/12 1:05
 **/
public final class ElementUtil {

	/**
	 * 将元素封装到列表中
	 *
	 * @param o
	 * @return
	 */
	public static List<Object> elementTransfer(Object o) {
		// 将元素添加到列表中
		List<Object> elements = new ArrayList<>();
		if (o instanceof Collection) {
			Collection collection = (Collection) o;
			elements.addAll(collection);
		} else {
			elements.add(o);
		}
		return elements;
	}

	/**
	 * 元素类型转换
	 *
	 * @param removedElements
	 * @param type
	 * @return
	 */
	@SuppressWarnings("all")
	public static List transferElementType(ArrayList removedElements, EvictEqType evictEqType) {
		// 类型转换，redisTemplate有类型判断，需要和插入时的类型相同才可等值删除
		if (evictEqType == EvictEqType.LONG) {
			List<Long> transfer = new ArrayList<>(removedElements);
			return transfer;
		} else if (evictEqType == EvictEqType.DOUBLE) {
			List<Double> transfer = new ArrayList<>(removedElements);
			return transfer;
		} else if (evictEqType == EvictEqType.INTEGER) {
			List<Integer> transfer = new ArrayList<>(removedElements);
			return transfer;
		} else if (evictEqType == EvictEqType.FLOAT) {
			List<Float> transfer = new ArrayList<>(removedElements);
			return transfer;
		} else if (evictEqType == EvictEqType.BOOLEAN) {
			List<Boolean> transfer = new ArrayList<>(removedElements);
			return transfer;
		} else if (evictEqType == EvictEqType.BYTE) {
			List<Byte> transfer = new ArrayList<>(removedElements);
			return transfer;
		} else if (evictEqType == EvictEqType.STRING) {
			List<String> transfer = new ArrayList<>(removedElements);
			return transfer;
		}
		List<String> transfer = new ArrayList<>();
		return transfer;
	}
}
